package com.xxl.conf.core.util;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.core.CollectionUtils;

/**
 * 环境基类
 * @author xuxueli 2015-8-28 10:37:43
 */
public class Environment {
	
	private static Logger logger = LoggerFactory.getLogger(Environment.class);


	/**
	 * conf data path in zk
     */
	public static final String CONF_DATA_PATH = "/xxl-conf";

	/**
	 * zk config file
     */
	private static final String ZK_ADDRESS_FILE = "zk-conf.properties";

	/**
	 * zk address
     */
	public static final String ZK_ADDRESS;		// zk地址：格式	ip1:port,ip2:port,ip3:port
	
	static {
		Properties prop =  getProp();
		ZK_ADDRESS = prop.getProperty("zkserver");		
	}
	
	private static Properties getProp(){
		Properties prop = new Properties();
		InputStream in = null;
		try {
			ClassLoader loder = Thread.currentThread().getContextClassLoader();
			String path = loder.getResource(ZK_ADDRESS_FILE).getPath(); // 方式1：配置更新不需要重启JVM
			if (path!=null&&!path.equals("")) {
				in = new FileInputStream(path);
				// in = loder.getResourceAsStream(propertyFileName); // 方式2：配置更新需重启JVM
				if (in != null) {
					prop.load(in);
				}
			}
		} catch (IOException e) {
			logger.error("load {} error!", ZK_ADDRESS_FILE);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error("close {} error!", ZK_ADDRESS_FILE);
				}
			}
		}
		return prop;
	}

	public static void main(String[] args) {
		System.out.println(ZK_ADDRESS);
	}
}

